<template>
  <div class="wrap">
    <!-- 搜索 -->
    <div class="searchBox">
      <el-input
        prefix-icon="el-icon-search"
        v-model="searchTableInfo"
        placeholder="请输入搜索内容"
        style="width:30%"
        clearable
      ></el-input>
    </div>
    <!-- 列表 -->
    <el-table
      ref="table"
      :data="tableData.slice((currpage - 1) * pagesize, currpage * pagesize)"
      border
      style="width: 100%"
      stripe
      @selection-change="handleSelectionChange"
      @sort-change="changeSort"
      :row-key="getRowKeys"
      :cell-style="cellStyle"
    >
      <el-table-column
        type="selection"
        width="40"
        align="center"
        :reserve-selection="true"
      ></el-table-column>
      <el-table-column
        prop="user_for_gateway"
        label="用户名"
        min-width="200"
        align="center"
      >
        <template slot-scope="scope">
          <div @click="goMwDevice(scope.row)">
            {{ decodeURI(scope.row.user_for_gateway) }}
          </div>
        </template>
      </el-table-column>
      <el-table-column
        prop="device_code_for_gateway"
        label="设备编码"
        min-width="160"
        align="center"
      >
        <template slot-scope="scope">{{
          decodeURI(scope.row.device_code_for_gateway)
        }}</template>
      </el-table-column>
      <el-table-column
        prop="alias"
        label="网关别名"
        min-width="160"
        align="center"
      >
        <template slot-scope="scope">{{ decodeURI(scope.row.alias) }}</template>
      </el-table-column>
      <el-table-column
        prop="created_time"
        label="注册时间"
        min-width="160"
        align="center"
        sortable
      >
        <template slot-scope="scope">{{
          decodeURI(scope.row.created_time)
        }}</template>
      </el-table-column>

      <el-table-column
        class="play-box"
        label="操作"
        min-width="100"
        align="center"
        fixed="right"
        v-if="permissionData.hideActionArr.length < 2"
      >
        <template slot-scope="scope">
          <el-button
            size="small"
            type="primary"
            @click="modifyFn(scope.$index, scope.row)"
            v-permissions="{
              url: ['mwgateway/update', 'mwgateway/get'],
              index: scope.$index,
              key: 'hideAction',
              id: 'xiugai',
            }"
            >修改</el-button
          >
          <el-button
            size="small"
            type="danger"
            @click="delRow(scope.$index, scope.row)"
            v-permissions="{
              url: ['mwgateway/delete'],
              index: scope.$index,
              key: 'hideAction',
              id: 'shanchu',
            }"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <!--分页-->
    <el-pagination
      right
      background
      layout="total,prev, pager, next, sizes,jumper"
      :page-sizes="[5, 10, 15, 25]"
      :page-size="pagesize"
      :total="tableData.length"
      @current-change="handleCurrentChange"
      @size-change="handleSizeChange"
    ></el-pagination>
    <!-- 点击修改，弹出框 -->
    <mwGatewayModify
      :dialogVisible="dialogVisible"
      :user_for_gateway="user_for_gateway"
      v-if="dialogVisible.show"
      @refreshTableList="refreshTableList"
    />
  </div>
</template>

<script>
import { getData, postData } from "@/request/http";
import mwGatewayModify from "./mwGatewayModify";
export default {
  name: "",
  components: {
    mwGatewayModify,
  },
  props: {},
  data() {
    return {
      currpage: 1, //分页
      pagesize: 10, //分页
      searchTableInfo: "", //搜索条件
      getSearchInfo: [], //搜索后，过滤后的tableData数据
      tableChecked: [], // 多选
      batchDelectId: [], //批量删除id
      checkAll: false, // 是否全选状态
      //修改弹出框
      dialogVisible: {
        show: false,
      },
      user_for_gateway: "", //打开弹窗，当前行id
      permissionData: {
        hideActionArr: [], // 操作按钮的隐藏
      },
    };
  },
  computed: {
    // 根据计算属性模糊搜索
    tableData() {
      const searchTableInfo = this.searchTableInfo;
      //console.log(searchTableInfo);
      if (searchTableInfo) {
        return this.getSearchInfo.filter((item) => {
          return Object.keys(item).some((key) => {
            return (
              String(item[key])
                .toLowerCase()
                .indexOf(searchTableInfo) > -1
            );
          });
        });
      }
      return this.getSearchInfo;
    },
    //刷新table
    submitData() {
      return this.$store.state.submitData;
    },
  },
  watch: {
    //刷新table
    submitData(newVal, oldVal) {
      if (newVal != oldVal) {
        this.getList();
        this.currpage = 1;
      }
    },
  },
  created() {},
  mounted() {
    this.getList();
  },
  methods: {
    //获取初始列表数据
    getList() {
      postData("/adminweb/mwgateway/list", {})
        .then((res) => {
         // console.log(res);
          if (res.code == 0) {
            let gateways = res.datas.gateways;
            this.getSearchInfo = gateways;
          } else if (res.code == -56055) {
            return;
          } else {
            this.$message.error(res.code + "，" + res.msg);
          }
        })
        .catch((res) => {
          this.$message.error(res.code + "，" + res.msg);
        });
    },

    //删除单行
    delRow(index, rows) {
     // console.log(rows);
      var subArr = [];
      subArr.push(rows.user_for_gateway);
      var that = this;
      that
        .$confirm(
          "此操作将删除:" + rows.user_for_gateway + ", 是否继续?",
          "删除",
          {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning",
          }
        )
        .then(() => {
          postData("/adminweb/mwgateway/delete", {
            users_for_gateway: subArr,
          })
            .then((res) => {
              that.logining = false;
              //console.log(res);
              if (res.code == "0") {
                that.$message({
                  type: "success",
                  message: "删除成功!",
                });
                that.getList();
              } else {
                that.$message.error(res.code + ":" + res.msg);
              }
            })
            .catch((res) => {
              that.$message.error(res);
            });
        })
        .catch((res) => {
          that.$message.success("已取消");
        });
    },
    //点击修改按钮，弹窗
    modifyFn(index, row) {
      this.dialogVisible = { show: true };
      this.user_for_gateway = row.user_for_gateway;
    },
    //点击开发者id，去该开发者的应用
    goMwDevice(val) {
     // console.log(val);
      var user_for_gateway = val.user_for_gateway;
      this.$router.push({
        path: "/mwGateway/mwGatewayDeviceList",
        query: { user_for_gateway: user_for_gateway },
      });
    },
    //table第一列小手样式
    cellStyle(row) {
      if (row.columnIndex === 1) {
        return "color:#3182bd;cursor: pointer";
      }
    },
    // 获取row的key值
    getRowKeys(row) {
      // return row.org_id;
    },
    //点击 第几页
    handleCurrentChange(cpage) {
      this.currpage = cpage;
    },
    //每页有几条
    handleSizeChange(psize) {
      this.pagesize = psize;
    },
    //操作多选
    handleSelectionChange(val) {
      var that = this;
      this.batchDelectId = [];
      that.tableChecked = val;
      //console.log(that.tableChecked);
      if (val.length > 0) {
        for (let i = 0; i < val.length; i++) {
          this.batchDelectId.push(val[i]["role_id"]);
        }
      }
    },
    //排序
    changeSort(val) {
      //console.log(val);
    },
    refreshTableList() {
      this.getList();
    },
  },
};
</script>
<style scoped>
/* 更改table,表头背景 */
.wrap >>> .el-table--border th,
.el-table__fixed-right-patch {
  /* background: #c0dee2; */
  background: #f0f3f4;
}
.wrap {
  margin-top: 20px;
  padding: 15px;
  overflow: hidden;
}
.searchBox {
  margin-bottom: 20px;
}
.wrap >>> .el-pagination {
  float: right;
  margin-top: 30px;
}

.batch-btn-con {
  margin-top: 70px;
}
.wrap >>> .tips {
  margin-top: 10px;
  display: block;
  font-size: 15px;
}
</style>
